import 'dart:io';

import 'package:cms/cms.dart';
import 'package:common/common.dart';
import 'package:common/export/bloc.dart';
import 'package:common/export/tools.dart';
import 'package:common/export/get_it.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import 'package:larapress_app/routes.dart';
import 'package:theme/state/theme/theme_cubit.dart';
import 'package:theme/theme.dart';
import 'package:user/state/user_action_history/user_action_history_cubit.dart';
import 'package:user/user.dart';

class DevHttpOverrides extends HttpOverrides {
  @override
  HttpClient createHttpClient(SecurityContext? context) {
    return super.createHttpClient(context)
      ..badCertificateCallback = (X509Certificate cert, String host, int port) {
        // 在开发环境中忽略证书验证
        return kDebugMode;
      };
  }
}

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  if (kDebugMode) {
    HttpOverrides.global = DevHttpOverrides();
  }
  LaraPressCommon.init();
  LaraPressTheme.setupLaraPressTheme();
  LaraPressUser.setupLaraPressUser();
  LaraPressCms.setupLaraPressCmsCubit();
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(375, 667),
      splitScreenMode: false,
      minTextAdapt: false,
      builder: (context, child) {
        return MultiBlocProvider(
          providers: [
            BlocProvider<LaraPressThemeCubit>(
              create: (context) => laraPressGetIt<LaraPressThemeCubit>(),
            ),
            BlocProvider<LaraPressUserActionHistoryCubit>(
              create: (context) =>
                  laraPressGetIt<LaraPressUserActionHistoryCubit>(),
            ),
          ],
          child: MaterialApp.router(
            title: 'Flutter Demo',
            theme: LaraPressTheme.createTheme(),
            debugShowCheckedModeBanner: false,
            routerConfig: laraPressAppRouter,
          ),
        );
      },
    );
  }
}
